<?php

namespace Wechat\Controller;

class IndexController extends CommonController  {

    public function index() {

        $categoryMap["is_online"] = C("DEFAULT_YN.YES");
        $categoryList = D("Category")
            ->where($categoryMap)->select();

        $bannerMap["banner_type"] = C("BANNER_TYPE.HOME");
        $bannerMap["is_online"] = C("DEFAULT_YN.YES");
        $bannerList = D("Banner")
            ->where($bannerMap)->select();

        $announcementMap["is_online"] = C("DEFAULT_YN.YES");
        $announcementList = D("Announcement")
            ->where($announcementMap)
            ->order("announcement_id desc")
            ->select();

        $guideList = D("Guide")->select();

        $this->assign("guideList", $guideList);
        $this->assign("announcementList", $announcementList);
        $this->assign("bannerList", $bannerList);
        $this->assign("categoryList", $categoryList);
        $this->assign("signature",signature());
        $this->assign("appid",C('appid'));
        $this->assign("shareUrl",'http://'.$_SERVER['HTTP_HOST'].U('Index/index'));

        $this->display("index");

    }

    public function productList() {

        $categoryId = I("post.categoryId");
        $pageNum = I("post.pageNum") ? I("post.pageNum") : 1;

        if(!isNullOrEmpty($categoryId)) {

            $category = D("Category")->queryById($categoryId);

            if(!isNullOrEmpty($category)
                && $category["is_online"] == C("DEFAULT_YN.YES")) {
                $map["c.category_id"] = $categoryId;
            }

        } else {
            $map["p.show_index"] = C("DEFAULT_YN.YES");
        }

        $map["p.is_online"] = C("DEFAULT_YN.YES");

        $list = D("Product")->alias("p")
            ->join("left join __CATEGORY__ c on c.category_id = p.category_id")
            ->join("left join __ROUND__ r on r.product_id = p.product_id and r.status = 1")
            ->where($map)
            ->field("p.current_round, p.end_time, p.product_name,p.product_id,
                p.image, p.price, r.join_number, r.group_number, r.round_id")
            ->order("p.end_time asc")
            ->limit(($pageNum-1)*C("DEFAULT_PAGE_SIZE"), C("DEFAULT_PAGE_SIZE"))
            ->select();

        echo json_encode($list);

    }


}